package cn.tedu.zx.ums.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.io.Serializable;

/**
 * 定义redis用到的redisTemplate对象序列化
 */
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate<String, Serializable> initRedisTemplate(
            RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Serializable> redisTemplate=new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置以下序列化器，RedisTemplate可以自动处理Java对象与Redis之间的序列化和反序列化。
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}
